package com.hp.dingding.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.dingding.entity.Employee;
import com.hp.dingding.entity.Note;
import com.hp.dingding.service.NoteService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class NoteController {

    @Resource
    private NoteService noteService;


    @RequestMapping("check")
    public String checkNote(Note note,Model model){
        System.out.println(note);
         Integer result = noteService.checkNote(note);
         if(result!=null){
             model.addAttribute("msg","已处理!");
         }else{
             model.addAttribute("msg","出错了!");
         }
        return "leave";
    }




    @RequestMapping("addNote")
    public String addNote(Note note,Model model){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        note.setSubdate(format);
        note.setEstatus(0);
        System.out.println(note);
        Integer result = noteService.addNote(note);
        if(result!=null){
            model.addAttribute("msg","申请成功，待领导审核!");
        }else {
            model.addAttribute("msg","数据不完整，请重新填写!");
        }

        return "leave";
    }



    @RequestMapping("leave")
    public String leave(HttpServletRequest request, Model model,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "0")Integer eid,Integer estatus){
        PageHelper.startPage(pageNum,5);
        List<Note> noteList = noteService.queryAll(eid);
        PageInfo<Note> notePageInfo = new PageInfo<>(noteList, 5);
        if(!noteList.isEmpty()){
            model.addAttribute("notes",notePageInfo);
        }else {
            model.addAttribute("msg","没有记录");
        }
        return "leave";
    }




}
